﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using BluePortal.LiveID;
using BluePortal.Medias.Core;
using BluePortal.Framework;

namespace BluePortal.QuikStart
{
    public partial class EditUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    tbLoginName.Text = User.Identity.Name;
                    tbLoginName.Enabled = false;
                    BluePortalMemberShipProvider ms = (BluePortalMemberShipProvider)Membership.Provider;
                    if (ms.GetUser(User.Identity.Name, false) != null)
                        tbEmail.Text = ms.GetUser(User.Identity.Name, false).Email;
                    else
                    {
                        tbEmail.Text = "Email Domain";
                        tbEmail.Enabled = false;
                    }

                    if (System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue is DateTime)
                       tbAge.Text = ((DateTime)System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue).ToShortDateString();
                    if (System.Web.Profile.DefaultProfile.Properties["Location"].DefaultValue != null)
                        tbLocation.Text = System.Web.Profile.DefaultProfile.Properties["Location"].DefaultValue.ToString();
                }
                rvDateOfBirth.MaximumValue = DateTime.Now.AddYears(-13).ToShortDateString();
                rvDateOfBirth.MinimumValue = DateTime.MinValue.ToShortDateString();
            }
            myAllVideos.VideosList = new BCMedia().FindAllByUserWithViewCount(Page.User.Identity.Name, new FindParameters() { Count = 30});
            Channel1.ChannelsList = new BCChannel().FindByOwnerWithMedia(Page.User.Identity.Name, new FindParameters() { Count = 30 });                      
        }

        protected void btUpdateUser_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                if (Session["NewLiveId"] != null)
                {
                    BluePortalMemberShipProvider ms = (BluePortalMemberShipProvider)Membership.Provider;
                    MembershipCreateStatus status;
                    ms.CreateUser(tbLoginName.Text, "C!Secret", tbEmail.Text, null, null, true, null, out status);
                    if (status == MembershipCreateStatus.Success)
                    {
                        LiveIdService ls = new LiveIdService();
                        ls.AddLoginName((string)Session["NewLiveId"], tbLoginName.Text);
                        Session["NewLiveId"] = null;
                        FormsAuthentication.RedirectFromLoginPage(tbLoginName.Text, false);
                        if (tbAge.Text != String.Empty)
                            System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue = DateTime.Parse(tbAge.Text);
                        else
                            System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue = DateTime.MaxValue;
                        System.Web.Profile.DefaultProfile.Properties["Location"].DefaultValue = tbLocation.Text;

                        Response.Redirect("/default.aspx");
                    }
                    else
                    {
                        cvUser.IsValid = false;
                        cvUser.Text = status.ToString();
                    }
                }
            }
            else
            {
                BluePortalMemberShipProvider ms = (BluePortalMemberShipProvider)Membership.Provider;
                MembershipUser msu = ms.GetUser(Context.User.Identity.Name, false);
                if (msu != null)
                {
                    msu.Email = tbEmail.Text;
                    ms.UpdateUser(msu);
                }
                if (tbAge.Text != String.Empty)
                    System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue = DateTime.Parse(tbAge.Text);
                else
                    System.Web.Profile.DefaultProfile.Properties["Age"].DefaultValue = DateTime.MaxValue;
                System.Web.Profile.DefaultProfile.Properties["Location"].DefaultValue = tbLocation.Text;
                
            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            BluePortalMemberShipProvider ms = (BluePortalMemberShipProvider)Membership.Provider;
            ms.DeleteUser(User.Identity.Name, true);
            AuthenticationSection authenticationSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
            if (authenticationSection.Mode == AuthenticationMode.Forms)
                ((BluePortalMaster)Page.Master).SignOut();
            else
                Response.Redirect("Default.aspx");
        }

        protected void btUpload_Click(object sender, EventArgs e)
        {
            if (fuAvatar.FileName != string.Empty)
            {
                BluePortalMemberShipProvider ms = (BluePortalMemberShipProvider)Membership.Provider;
                ms.ChangeAvatar(User.Identity.Name, fuAvatar.FileBytes);
            }
        }
    }
}
